package handler

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"online-retailers/global"
	"online-retailers/internal/api/request"
	user1 "online-retailers/internal/proto/user"
)

func AddOrder(c *gin.Context) {
	var req request.AddOrder
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "数据不能为空",
		})
		return
	}
	_, err = global.UserClient.AddOrder(context.Background(), &user1.AddOrderReq{
		UserId:  int32(req.UserId),
		GoodsId: int32(req.GoodsId),
		Way:     int32(req.Way),
		Price:   int32(req.Price),
		Num:     int32(req.Num),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "创建订单失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "创建订单成功",
	})
}
